<?
if (!defined('IN_RJ_ADMIN')) die("Error 404");

$edit_url = 'index.php?act=ads&mode=edit';

$inp_arr = array(
		'name'	=> array(
			'table'	=>	'ads_web',
			'name'	=>	$lang_acp['web_ads_name'],
			'type'	=>	'free'
		),
		'url'	=> array(
			'table'	=>	'ads_url',
			'name'	=>	$lang_acp['link'],
			'type'	=>	'free'
		),
		'logo'	=> array(
			'table'	=>	'ads_img',
			'name'	=>	$lang_acp['logo'],
			'type'	=>	'free',
			'can_be_empty'	=> true,
		),
	);
##################################################
# ADD ADS
##################################################
if ($mode == 'add') {
	acp_check_permission('add_link');
	if ($_POST['submit']) {
		$error_arr = array();
		$error_arr = $form->checkForm($inp_arr);
		if (!$error_arr) {
			$sql = $form->createSQL(array('INSERT',$conf['prefix'].'ads'),$inp_arr);
			eval('$DB->query("'.$sql.'");');
			echo $lang_acp['added']." <meta http-equiv='refresh' content='1;url=$link'>";
			exit();
		}
	}
	$warn = $form->getWarnString($error_arr);

	$form->createForm($lang_acp['add_ads'],$inp_arr,$error_arr);
}
##################################################
# EDIT ADS
##################################################
if ($mode == 'edit') {
	acp_check_permission('edit_link');
	if ($ads_id) {
		if (!$_POST['submit']) {
			$q = $DB->query("SELECT * FROM ".$conf['prefix']."ads WHERE ads_id = '$ads_id'");
			$r = $DB->fetch_row($q);
			
			foreach ($inp_arr as $key=>$arr) $$key = $r[$arr['table']];
		}
		else {
			$error_arr = array();
			$error_arr = $form->checkForm($inp_arr);
			if (!$error_arr) {
				$sql = $form->createSQL(array('UPDATE',$conf['prefix'].'ads','ads_id','ads_id'),$inp_arr);
				eval('$DB->query("'.$sql.'");');
				echo $lang_acp['edited']." <meta http-equiv='refresh' content='1;url=".$edit_url."'>";
				exit();
			}
		}
		$warn = $form->getWarnString($error_arr);
		$form->createForm($lang_acp['edit_ads'],$inp_arr,$error_arr);
	}
	else {
		echo "<script>function check_del(id) {".
		"if (confirm('{$lang_acp['ask_u_del']} ?')) location='?act=ads&mode=del&ads_id='+id;".
		"return false;}</script>";
		echo "<table width=90% align=center cellpadding=2 cellspacing=0 class=border><form method=post>";
		echo "<tr><td align=left width=40% class=title>Web</td><td class=title width=40%>URL</td><td align=center class=title>Logo</td></tr>";
		$q = $DB->query("SELECT * FROM ".$conf['prefix']."ads ORDER BY ads_id ASC");
		while ($r = $DB->fetch_row($q)) {
			echo "<tr align=center class=fr><td><a href='?act=ads&mode=del&ads_id=".$r['ads_id']."'>{$lang_acp['del']}</a> - <a href=\"$link&ads_id=".$r['ads_id']."\"><b>".$r['ads_web']."</b></a></td><td class=fr_2><a href=\"".$r['ads_url']."\" target=_blank><b>".$r['ads_url']."</b></a></td><td class=fr><img src=\"".$r['ads_img']."\" width=140></td></tr>";
		}
		echo '<tr><td colspan="2" align="center"><input type="submit" name="sbm" class=submit value="'.$lang_acp['edit_order'].'"></td></tr>';
		echo '</form></table>';
	}
	
}
##################################################
# DELETE ADS
##################################################
if ($mode == 'del') {
	acp_check_permission('del_link');
	if ($ads_id) {
		if ($_POST['submit']) {
			$DB->query("DELETE FROM ".$conf['prefix']."ads WHERE ads_id = '".$ads_id."'");
			echo $lang_acp['deleted']." <meta http-equiv='refresh' content='1;url=".$edit_url."'>";
			exit();
		}
		?>
		<form method="post"><?=$lang_acp['ask_u_del']?> ??????<br><input value="<?=$lang_acp['yes']?>" name=submit type=submit class=submit></form>
<?
	}
}
?>